package com.yupi.cli.command;

import cn.hutool.core.bean.BeanUtil;
import com.yupi.model.DataModel;
import com.yupi.generator.MainGenerator;
import freemarker.template.TemplateException;
import lombok.Data;
import picocli.CommandLine;


import java.io.IOException;
import java.util.concurrent.Callable;


/**
 * @version 1.0
 * @Author yupi
 * @Date 2023-11-22
 * @注释 生成代码命令
 */
@CommandLine.Command(name="generate",mixinStandardHelpOptions = true)
@Data
public class GenerateCommand implements Callable<Integer> {


    @CommandLine.Option(names = {"--needGit"}, arity = "0..1", description = "是否生成.gitignore 文件", interactive = true, echo = true)
    private boolean needGit = true;


    @CommandLine.Option(names = {"-l", "--loop"}, arity = "0..1", description = "是否生成循环", interactive = true, echo = true)
    private boolean loop = false;


        /**
         * 核心模版
         */
        static DataModel.MainTemplate mainTemplate = new DataModel.MainTemplate();
        @CommandLine.Command(name = "mainTemplate")
        @Data
        public static class MainTemplateCommand implements Runnable {
        @CommandLine.Option(names = {"-a", "--author"}, arity = "0..1", description = "作者注释", interactive = true, echo = true)
        private String author = "yupi";
        @CommandLine.Option(names = {"-o", "--outputText"}, arity = "0..1", description = "输出信息", interactive = true, echo = true)
        private String outputText = "sum = ";

            @Override
            public void run() {
                mainTemplate.author = author;
                mainTemplate.outputText = outputText;
            }
        }


    public Integer call() throws Exception {
                if (loop) {
            System.out.println("输入核心模版配置：");
            CommandLine mainTemplateCommandLine = new CommandLine(MainTemplateCommand.class);
            mainTemplateCommandLine.execute("--author", "--outputText");
                }
        DataModel dataModel = new DataModel();
        BeanUtil.copyProperties(this, dataModel);
            dataModel.mainTemplate = mainTemplate;
        MainGenerator.doGenerate(dataModel);
        return 0;
    }
}
